//
//  MusicViewController.m
//  product_B
//
//  Created by lanou on 16/5/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MusicViewController.h"
#import "MusicListViewController.h"
#import "MoreMusicTableViewController.h"
@interface MusicViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)NSMutableArray *listMusicID;
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIView *thresholdView;
@property (nonatomic, assign)NSInteger index;
@property (nonatomic, strong)MusicListViewController *musicList;
@property (nonatomic, strong)MusicListViewController *music;
@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"音乐";
    self.navigationItem.title = @"音乐";
    _listMusicID = [NSMutableArray array];
    _index = 0;
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 49 - 64)];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    _scrollView.backgroundColor = UIColor.lightGrayColor;
    [self.view addSubview:self.scrollView];
    
    [self requestMusicList];
    
    
    
}



- (void)requestMusicList{
    [RequestManager initWithUrlString:@"http://v3.wufazhuce.com:8000/api/music/idlist/0" requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        _listMusicID = jsonDic[@"data"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self scrollViewAction];
            if (_listMusicID.count == 0) {
                return ;
            }
            
        });
    } errorBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)scrollViewAction{
    _scrollView.contentSize = CGSizeMake(kWidth * _listMusicID.count, 0);
    
    _musicList = [[MusicListViewController alloc]init];
    _musicList.musicID = _listMusicID[0];
    
    _musicList.view.frame = CGRectMake(0, 0, kWidth, kHeight - 64 - 49);
    [self addChildViewController:_musicList];
    [_scrollView addSubview:_musicList.view];
    
    
    
    
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger current = _scrollView.contentOffset.x / kWidth;
    
    if (_index >= current) {
        if (![[MusicPlayerManager shareInstance].playUrl isEqualToString:[MusicPlayerManager shareInstance].musicArray[current]]) {
            _musicList = [[MusicListViewController alloc]init];
            _musicList = self.childViewControllers[current];
            [_musicList.musicPlayerView.button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        }
    }else{
        _musicList = [[MusicListViewController alloc]init];
        _musicList.musicID = _listMusicID[current];
        _musicList.view.frame = CGRectMake(kWidth * current, 0, kWidth, kHeight - 64 - 49);
        [self addChildViewController:_musicList];
        [_scrollView addSubview:_musicList.view];
        _index = current;
        
    }
    
    _music = [[MusicListViewController alloc]init];
    _music = self.childViewControllers[current];
    [MusicPlayerManager shareInstance].url = _music.model.music_id;
    [MusicPlayerManager shareInstance].model = _musicList.model;
    
}



- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (_scrollView.contentOffset.x <= -60) {

        [[MusicPlayerManager shareInstance].musicArray removeAllObjects];
        [_listMusicID removeAllObjects];
        _index = 0;
        [self requestMusicList];
        
    }
    if (_scrollView.contentOffset.x > kWidth * (_listMusicID.count - 1)) {

        MoreMusicTableViewController *more = [[MoreMusicTableViewController alloc]init];
        [self.navigationController pushViewController:more animated:YES];
    }
    
}

- (void)musicButtonAction:(UIButton *)sender{
    [_musicList.tableView setContentOffset:CGPointMake(0, kWidth - 120) animated:YES];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    if ([[MusicPlayerManager shareInstance].playUrl isEqualToString:[MusicPlayerManager shareInstance].url]) {
        
        if ([MusicPlayerManager shareInstance].isPlay) {
            [_musicList.musicPlayerView.button setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
        }
    }
    
    if (![MusicPlayerManager shareInstance].isPlay) {
        for (MusicListViewController *music in self.childViewControllers) {
            [music.musicPlayerView.button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        }
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
